// pages/bind/bind.js
import Config from "../../common/config.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bindList: {},
    bookid: "",
    bindIndex: 0,
    bindingId: "",
    templateid: "",
    phone: '',//手机号
    code: '',//验证码
    codename: '获取验证码',
    codeState: true,
    isAuthorize: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log(options)
    that.setData({
      templateid: options.temId
    })
    var sessionId = wx.getStorageSync('sessionId');
    // wx.getStorageSync({
    //   key: 'templateId',
    //   success: function (res) {
    //     that.setData({
    //       templateid: res.data
    //     });
    //     console.log(that.data.templateid);
    //   },
    // }); 

    console.log(that.data.templateid);
    // 判断用户是否绑定手机号
    // wx.request({
    //   url: Config.url + '/api/user/getMobile',
    //   method: 'POST',
    //   header: {
    //     'content-type': 'application/json',
    //     'accessToken': wx.getStorageSync('sessionId')
    //   },
    //   data: {},
    //   success(res) {
    //     console.log(res)
    //     if (res.data.data.mobile == null) {
    //       wx.showModal({
    //         title: '提示',
    //         content: '是否绑定手机号',
    //         success(res) {
    //           if (res.confirm) {
    //             that.setData({
    //               codeState: false
    //             })

    //           } else {
    //             wx.navigateBack()
    //           }
    //         }
    //       })

    //     } else {
    //       // var sessionId = wx.getStorageSync('sessionId')
    //       // // 创建一本书返回id
    //       // wx.request({
    //       //   url: Config.url + '/api/book/save',
    //       //   method: 'POST',
    //       //   header: {
    //       //     'content-type': 'application/json',
    //       //     'accessToken': sessionId
    //       //   },
    //       //   data: {},
    //       //   success(res) {
    //       //     that.setData({
    //       //       bookId: res.data.data.book.id
    //       //     })
    //       //     wx.request({
    //       //       url: Config.url + '/api/book/save',
    //       //       method: 'POST',
    //       //       header: {
    //       //         'content-type': 'application/json',
    //       //         'accessToken': sessionId
    //       //       },
    //       //       data: { templateId: that.data.templateid, bookId: that.data.bookId },
    //       //       success(res) {

    //       //       }
    //       //     })
    //       //   }
    //       // })
    //     }
    //   }
    // }),
      // 获取装订列表
      wx.request({
        url: Config.url + '/api/makeBook/selectBind',
        method: 'POST',
        header: {
          'content-type': 'application/json',
          'accessToken': sessionId
        },
        data: {
          templateId: that.data.templateid
        },
        success(res) {
          console.log(res.data.data.data[0].bindingId)
          that.setData({
            bindList: res.data.data.data,
            bindingId: res.data.data.data[0].bindingId
          })
          var arrays = res.data.data.data

        }
      })

  },
  // 选择装订类型
  choseBind: function (e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      bindIndex: e.currentTarget.dataset.index,
      bindingId: e.currentTarget.dataset.id
    })
  },
  //跳转详情
  gobindingDetails: function (e) {
    console.log(e.currentTarget.dataset.templateid)
    wx.navigateTo({
      url: '../bindingDetails/bindingDetails?bindingid=' + e.currentTarget.dataset.bookid + "&templateid=" + e.currentTarget.dataset.templateid,
    })
  },
  // 跳转作书
  begin: function () {
    var that = this
    var sessionId = wx.getStorageSync('sessionId')
    wx.request({
      url: Config.url + '/api/book/save',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'accessToken': wx.getStorageSync('sessionId')
      },
      data: {
        bindingId: that.data.bindingId,
        templateId: that.data.templateid
      },
      success(res) {
        console.log(that.data.bookid);
        wx.navigateTo({
          url: '../book/book?bookId=' + res.data.data.book.id
        })

      }
    })
  },
  //获取input输入框的值
  getPhoneValue: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getCodeValue: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 获取验证码
  getCode: function () {
    var a = this.data.phone;
    var that = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      wx.request({
        url: Config.url + '/api/user/SMS_phoneValidate',
        method: 'get',
        header: {
          'content-type': 'application/json',
          'accessToken': wx.getStorageSync('sessionId')
        },
        data: { teleNum: that.data.phone },
        success(res) {
          console.log(res)
          that.setData({
            iscode: res.data.data,
            disabled: true
          })
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              that.setData({
                codename: '重新发送',
                disabled: false
              })

            } else {
              that.setData({
                codename: num + "s"
              })
            }
          }, 1000)
        }
      })

    }


  },
  //获取验证码按钮
  getVerificationCode() {
    this.getCode();
    var that = this

  },
  //提交表单信息
  save: function () {
    var that = this
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (this.data.code == "") {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      wx.request({
        url: Config.url + '/api/user/SMS_verification',
        method: 'POST',
        header: {
          'content-type': 'application/json',
          'accessToken': wx.getStorageSync('sessionId')
        },
        data: { mobile: that.data.phone, code: that.data.code },
        success(res) {
          console.log(res.data)
          if (res.data.code == 312) {
            wx.showToast({
              title: res.data.data.msg,
              icon: 'none',
              duration: 1000
            })
          }
          if (res.data.code == 100) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 1000
            })
            var mobile = res.data.data.mobile
            wx.request({
              url: Config.url + '/api/user/updateData',
              method: 'POST',
              header: {
                'content-type': 'application/json',
                'accessToken': wx.getStorageSync('sessionId')
              },
              data: { mobile: mobile },
              success(res) {
                console.log(res)
                that.setData({
                  codeState: true
                })
              }
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  authorize: function (res) {
    if (res.detail.errMsg == 'getUserInfo:ok') {
      this.begin()
    }
  }
})